<template>
    <div style="text-align: center;margin: 0 20px">
        <div style="margin-top: 150px;">
          <div style="font-size: 25px;font-weight: bold">login</div>
          <div style="font-size: 14px;color: grey">在进入系统之前请先输入用户名和密码进行登录</div>
        </div>
        <div style="margin-top: 50px">
          <el-input v-model="form.username" type="text" placeholder="user/email">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
          <el-input v-model="form.password" type="password" style="margin-top: 10px;" placeholder="pwd">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </div>
        <el-row style="margin-top: 5px">
          <el-col :span="12" style="text-align: left;">
            <el-checkbox v-model="form.remember" label="remember me" />
          </el-col>
          <el-col :span="12" style="text-align: right;">
            <el-link @click="router.push('/forget')">forget pwd</el-link>
          </el-col>
        </el-row>
        <div style="margin-top: 40px">
          <el-button @click="login()" style="width: 270px" type="success" plain>
            login
          </el-button>
        </div>
        <el-divider>
          <span style="color: grey;font-size: 13px;">no account</span>
        </el-divider>
        <div>
          <el-button style="width:270px" @click="router.push('/register')" type="warning" plain>
            register
          </el-button>
        </div>
    </div>
</template>

<script setup>
import {User, Lock} from '@element-plus/icons-vue';
import {reactive} from "vue";
import { get, post} from '@/net'
import router from "@/router";
import { useStore } from '@/stores';

const store = useStore()

const form = reactive({
  username: '',
  password: '',
  remember: false
})

const login = () => {
  console.log(!form.password)
  if(!form.username || !form.password) {
    console.log('el-message...')
    ElMessage.warning('username and password are required')
  } else {
    post('/api/auth/login', {
      username: form.username,
      password: form.password,
      remember: form.remember
    }, (message) => {
      ElMessage.success(message)
      get('/api/user/me', (message) => {
        store.auth.user = message
        router.push('/index')
      }, () => {
        store.auth.user = null
      })
    })
  }
}
</script>

<style lang="css" scoped>
</style>
